package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.http.entity.MonthBean;
import com.weimei.conversionTreasure.http.entity.MonthDetailBean;
import com.weimei.conversionTreasure.http.entity.YMonthDetailBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.weight.BottomTimePop;
import com.weimei.conversionTreasure.weight.BottomWheelPop;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class YearDetailVM extends TitleVM{
    public YearDetailVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("账单详情");
    }

    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }
    public ObservableField<String> curY = new ObservableField<>();

    public ObservableField<String> time = new ObservableField<>("0");
    public ObservableField<String> types = new ObservableField<>("支出");
    public ObservableField<String> orderby = new ObservableField<>("降序");
    public ObservableField<String> zZcT = new ObservableField<>("总支出");
    public ObservableField<String> zZc = new ObservableField<>("");
    public ObservableField<String> zBs = new ObservableField<>("");


    public BindingCommand typePick=  new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            List<String> list = new ArrayList<>();
            list.add("降序");
            list.add("升序");
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomWheelPop(mActivity,list, s1 -> {
                        orderby.set(s1);
                        onResume();
                    }))
                    .show();
        }
    });

    public BindingCommand leftClick = new BindingCommand(() -> {
        int js = Integer.parseInt(time.get()) -1;
        if (js > 0 && js < 10){
            time.set("0"+js);
        }else {
            time.set(js+"");
        }
        onResume();

    });

    public BindingCommand rightClick = new BindingCommand(() -> {
        int js = Integer.parseInt(time.get())+1;
        if (js <= 12 && js >= 10){
            time.set(js+"");
        }else {
            time.set("0"+js);
        }

        onResume();
    });

    public BindingCommand timePick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            new XPopup.Builder(mActivity)
                    .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                    .enableDrag(true)
                    .asCustom(new BottomTimePop(mActivity, s1 -> {
                        time.set(s1);
                        onResume();
                    },true,false))
                    .show();
        }
    });

    @Override
    public void onResume() {
        super.onResume();
        getYMData();
    }

    public ObservableList<YMonthDetailItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<YMonthDetailItemVM> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_year_detail);

    public void getYMData(){
        zZcT.set("总"+types.get());
        observableList.clear();
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("date", curY.get()+time.get());
        map.put("types", types.get());
        map.put("orderby", orderby.get());

        model.month_chart(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<YMonthDetailBean>() {
                    @Override
                    public void onNext(YMonthDetailBean bean) {
                        if (bean.issucc){
                            zZc.set(bean.getData().getPriceSum()+"");
                            zBs.set(bean.getData().getTotalNumber()+"");
                            if (bean.getData().getRDay().size() > 0){
                                for (YMonthDetailBean.DataDTO.RDayDTO data: bean.getData().getRDay()) {
                                    YMonthDetailItemVM itemVM = new YMonthDetailItemVM(YearDetailVM.this,data);
                                    observableList.add(itemVM);
                                }
                            }
                        }

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
